package org.vdab.projects.tasks.client.gui;

import org.vdab.projects.tasks.client.entities.ClientTaskBook;
import org.vdab.projects.tasks.client.entities.ClientTaskList;
import org.vdab.projects.tasks.client.gui.widgets.EntityListWidget;
import org.vdab.projects.tasks.client.util.DelayedList;
import org.vdab.projects.tasks.client.util.ICommandWithReturn;
import org.vdab.projects.tasks.client.util.MessageGuard;
import org.vdab.projects.tasks.shared.entities.ITaskList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class TaskBookWidget extends Composite {

	private static TaskBookWidgetUiBinder uiBinder = GWT
			.create(TaskBookWidgetUiBinder.class);

	interface TaskBookWidgetUiBinder extends UiBinder<Widget, TaskBookWidget> {
	}

	private ClientTaskBook myTaskbook;
	
	private DelayedList<ClientTaskList> clientTaskListList = new DelayedList<ClientTaskList>();

	@UiField
	Label taskBookName;

	@UiField
	Button btnCreateList;
	
	@UiField
	EntityListWidget<ClientTaskList, TaskListWidget> tasksListList = new EntityListWidget<ClientTaskList, TaskListWidget>();
	
	public TaskBookWidget() {		
		MessageGuard.Log("in TaskBookWidget ctr");		
		initWidget(uiBinder.createAndBindUi(this));
		btnCreateList.setEnabled(false);			
	}

	@UiHandler("btnCreateList")
	void onClick(ClickEvent e) {	
		try {			
			ClientTaskList tl = this.myTaskbook.createNewTaskList();
			//this.clientTaskListList.add(tl);
		} catch (Exception ex) {
			MessageGuard.Log(ex.getLocalizedMessage());
		}		
	}
	
	private void bind() throws Exception{		
		this.tasksListList.bind(clientTaskListList, new ICommandWithReturn<ClientTaskList, TaskListWidget>() {
			@Override
			public TaskListWidget execute(ClientTaskList entity)
					throws Exception {		
				if(entity==null){
					MessageGuard.Log("entity was NULL when setting ClientTaskBook. (in TaskBookWidget)");
				}
				MessageGuard.Log("Creating TaskListWidget for " + entity.getDisplayText());
				return new TaskListWidget().set(entity);
			}
		});			
	}

	public void set(ClientTaskBook taskBook){
		MessageGuard.Log("In set() of TaskBookWidget.");
				
		this.myTaskbook = taskBook;
		btnCreateList.setEnabled(taskBook!=null);		

		try {		
			//Must it be bound every time??
			bind();	
			this.tasksListList.clear();
			taskBook.getClientTaskLists(clientTaskListList);
		} catch (Exception e) {
			MessageGuard.Log(e.getLocalizedMessage());
		}		
	}

	protected void update() {
		try {
			MessageGuard.Log("In update() of TaskBookWidget.");
			this.taskBookName.setText(myTaskbook.getDisplayText());			
		} catch (Exception e) {
			MessageGuard.Log(e.getLocalizedMessage());
		}
	}
}
